<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收费站" prop="deptId" v-if="deptId==101">
        <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
          class="margin-right" placeholder="请选择部门" check-strictly clearable @change="bindChangeTree($event,'deptId')" />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="radioType" class="demo-tabs" @tab-change="bindChangeType">
      <el-tab-pane label="班组例会" name="班组例会">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:classActive:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="classActiveList" @selection-change="handleSelectionChange" show-overflow-tooltip>
          <el-table-column type="index" width="65" label="序号" align="center" />
          <el-table-column label="收费站" align="center" prop="deptName" />
          <el-table-column label="班组名称" align="center" prop="className" />
          <el-table-column label="班组人数" align="center" prop="classNum" />
          <el-table-column label="班组建设制度" align="center" prop="classPlan">
            <template #default="scope">
              <el-button @click.stop="previewFun(scope.row.classPlan)" v-if="scope.row.classPlan">查看</el-button>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>
          <el-table-column label="开展时间" align="center" prop="carryTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.carryTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="会议主题" align="center" prop="topic" />
          <el-table-column label="会议地点" align="center" prop="address" />
          <el-table-column label="班组例会记录" align="center" prop="record">
            <template #default="scope">
              <el-button @click.stop="previewFun(scope.row.record)" v-if="scope.row.record">查看</el-button>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>

          <el-table-column label="上传人" align="center" prop="reporter" />
          <el-table-column label="上传时间" align="center" prop="reportTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="班组活动" name="班组活动">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:classActive:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="classActiveList" @selection-change="handleSelectionChange">
          <el-table-column type="index" width="65" label="序号" align="center" />
          <el-table-column label="收费站" align="center" prop="deptName" />
          <el-table-column label="班组名称" align="center" prop="className" />
          <el-table-column label="班组人数" align="center" prop="classNum" />
          <el-table-column label="班组建设制度" align="center" prop="classPlan">
            <template #default="scope">
              <el-button @click.stop="previewFun(scope.row.classPlan)" v-if="scope.row.classPlan">查看</el-button>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>
          <el-table-column label="活动时间" align="center" prop="carryTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.carryTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="活动主题" align="center" prop="topic" />
          <el-table-column label="活动地点" align="center" prop="address" />
          <el-table-column label="班组活动记录表" align="center" prop="record">
            <template #default="scope">
              <el-button @click.stop="previewFun(scope.row.record)" v-if="scope.row.record">查看</el-button>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>
          <el-table-column label="班组活动总结" align="center" prop="summary">
            <template #default="scope">
              <el-button @click.stop="previewFun(scope.row.summary)" v-if="scope.row.summary">查看</el-button>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>
          <el-table-column label="活动照片" align="center" prop="img" width="100">
            <template #default="scope">
              <image-preview :src="scope.row.img" :width="50" :height="50" />
            </template>
          </el-table-column>
          <el-table-column label="上传人" align="center" prop="reporter" />
          <el-table-column label="上传时间" align="center" prop="reportTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>

      </el-tab-pane>
    </el-tabs>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <Viewer ref="fileViewerRef" :dialog-doc="dialogDoc" />

  </div>
</template>

<script setup name="ClassActive">
import { listClassActive, getClassActive, delClassActive, addClassActive, updateClassActive } from "@/api/coordination/class";
import Viewer from "@/components/Custom/viewer.vue";

import { getCKInfo } from '@/utils/auth'
import { deptTreeSelect } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const classActiveList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCarryTime = ref([]);
const deptOptions = ref([])
const deptId = ref(Number(getCKInfo('deptId')))
const fileViewerRef = ref()
const dialogDoc = ref(false)
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const radioType = ref("班组例会")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: deptId.value,
  },
  rules: {
    classId: [
      { required: true, message: "班组id(外键)不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

function isObject(value) {
  return typeof value === 'object' && value !== null;
}
/** 查询班组建设 活动或会议列表 */
function getList(type) {
  loading.value = true;
  listClassActive({ ...queryParams.value, pageType:isObject(type)?1:type?type:1 }).then(response => {
    classActiveList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

function bindChangeType(val) {

  getList(val == '班组例会' ? 1 : 2)

}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pageType: null
  };
  proxy.resetForm("classActiveRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCarryTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加班组建设 活动或会议";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getClassActive(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改班组建设 活动或会议";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["classActiveRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateClassActive(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClassActive(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除班组建设 活动或会议编号为"' + _ids + '"的数据项？').then(function () {
    return delClassActive(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/classActive/export', {
    ...queryParams.value,pageType:radioType.value=="班组例会"?1:2
  }, `${radioType.value}_${new Date().getTime()}.xlsx`)
}
// 预览
const previewFun = (file) => {
  dialogDoc.value = true;
  let data = {};
  // 上传过的文件组成完成的网络路径
  if (file&&file.indexOf(',')==-1) {
    data.src = `${baseUrl}${file}`;
  }else if(file&&file.indexOf(',')!=-1){
    let src=file.split(',');
    let srcArr=[]
    src.forEach(item=>{srcArr.push(`${baseUrl}${item}`)})
    data.src = srcArr.join(',');
    file=srcArr[0]
  }

  const arr = file.split(".");
  const suffix = arr[arr.length - 1];
  if (suffix == "docx" || suffix == "doc") {
    fileViewerRef.value?.viewDocx(data, '班组建设');
  } else if (suffix == "xlsx" || suffix == "xls") {
    fileViewerRef.value?.viewXlsx(data);
  } else if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
    fileViewerRef.value?.viewImg(data);
  } else if (suffix == "pdf") {
    fileViewerRef.value?.viewPdf(data);
  }
};
getList();
getDeptTree()
</script>
